package com.fancy.learncenter.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.UnusedGoodsAdapter;
import com.fancy.learncenter.bean.BannerListDataBean;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.GoodsDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.view.CustomFootView;
import com.fancy.learncenter.view.CustomHeaderView;
import com.fancy.learncenter.view.PopupWindowCity;
import com.fancy.learncenter.view.PopupWindowEducationType;
import com.fancy.learncenter.view.PopupWindowGoodsType;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UnusedGoodsActivity extends BaseActivity {

    PopupWindowGoodsType goodsType;


    UnusedGoodsAdapter adapter;
    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.conditionView)
    LinearLayout conditionView;
    @Bind(R.id.localText)
    TextView localText;
    @Bind(R.id.localIcon)
    ImageView localIcon;
    @Bind(R.id.salaryText)
    TextView salaryText;
    @Bind(R.id.salaryIcon)
    ImageView salaryIcon;
    @Bind(R.id.timeText)
    TextView timeText;
    @Bind(R.id.timeIcon)
    ImageView timeIcon;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;
    @Bind(R.id.ad_simdraweeview)
    SimpleDraweeView adSimdraweeview;
    @Bind(R.id.close_gg)
    ImageView closeGg;
    @Bind(R.id.ad_LL)
    FrameLayout adLL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unused_goods, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("闲置物品");


        ArrayList<GoodsDataBean> itemDatas = new ArrayList<>();
        GridLayoutManager linearLayoutManager = new GridLayoutManager(this, 2);
        recycleView.setLayoutManager(linearLayoutManager);
        adapter = new UnusedGoodsAdapter(this, itemDatas);
        recycleView.setAdapter(adapter);

        popupWindowCity = new PopupWindowCity(this);
        popupWindowCity.setSelectCallBack(new PopupWindowCity.SelectCallBack() {
            @Override
            public void selectCallBack(String province, String city, String area, String id) {
                localText.setText(area);

                locationId = id;

                getDatas(true);
            }

        });

        goodsType = new PopupWindowGoodsType(this, conditionView);
        goodsType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
            @Override
            public void cancleCallBack() {

                salaryText.setTextColor(Color.parseColor("#5a6062"));
                salaryIcon.setImageResource(R.mipmap.ic_jiantouxia);
            }

            @Override
            public void selecCallBack(String content, String id) {
                salaryText.setText(content);

                goodsId = id;

                getDatas(true);
            }


        });


        adapter.setCustomLoadMoreView(new CustomFootView(this));

        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setCustomHeaderView(new CustomHeaderView(this));
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                LogUtil.MyLog("onLoadMore", "====onRefresh==");
                getDatas(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getDatas(false);
                LogUtil.MyLog("onLoadMore", "====onLoadMore==");
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });

        getDatas(true);
        bannerList();
    }

    String goodsId = "";
    String locationId = "";

    PopupWindowCity popupWindowCity;

    @OnClick({R.id.local, R.id.salary, R.id.time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.local:
                goodsType.dismiss();
                popupWindowCity.ShowPickerView();

                selectCondition(1);
                break;
            case R.id.salary:
                goodsType.showPopupWindow();
                selectCondition(2);
                break;
            case R.id.time:
                goodsType.dismiss();
                selectCondition(3);
                break;
        }
    }

    private void selectCondition(int index) {
        localText.setTextColor(Color.parseColor("#5a6062"));
        localIcon.setImageResource(R.mipmap.ic_jiantouxia);

        salaryText.setTextColor(Color.parseColor("#5a6062"));
        salaryIcon.setImageResource(R.mipmap.ic_jiantouxia);

        timeText.setTextColor(Color.parseColor("#5a6062"));
        timeIcon.setImageResource(R.mipmap.ic_jiantouxia);


        if (index == 1) {
//            localText.setTextColor(Color.parseColor("#ff8200"));
//            localIcon.setImageResource(R.mipmap.ic_jiantoushang);
        } else if (index == 2) {
            salaryText.setTextColor(Color.parseColor("#ff8200"));
            salaryIcon.setImageResource(R.mipmap.ic_jiantoushang);
        } else if (index == 3) {
            timeText.setTextColor(Color.parseColor("#ff8200"));
            timeIcon.setImageResource(R.mipmap.ic_jiantoushang);
        }
    }

    int page;

    private void getDatas(final boolean isRefresh) {
        Map map = new HashMap();

        if (isRefresh) {
            page = 0;
        } else {
            page++;
        }
        map.put("page", page + "");
        map.put("locationId", locationId + "");
        map.put("type", goodsId + "");
        HttpMehtod.getInstance().unUserGoods(map, new IdeaObserver<BaseDataBean<List<GoodsDataBean>>>(this, isRefresh) {

            @Override
            public void onSuccess(BaseDataBean<List<GoodsDataBean>> listBaseDataBean) {

                if (listBaseDataBean.getData() != null) {
                    adapter.notifyDataSetChanged((ArrayList<GoodsDataBean>) listBaseDataBean.getData());
                }
                if (isRefresh) {
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);
                    if (listBaseDataBean.getData() == null) {
                        adapter.notifyDataSetChanged(new ArrayList<GoodsDataBean>());
                        return;
                    }
                    adapter.notifyDataSetChanged((ArrayList<GoodsDataBean>) listBaseDataBean.getData());
                } else {
                    if (listBaseDataBean.getData() == null) {
                        xRefreshView.setLoadComplete(true);
                    } else if (listBaseDataBean.getData() != null && listBaseDataBean.getData().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.stopLoadMore();
                    }


                    if (listBaseDataBean.getData() == null) {
                        return;
                    }
                    adapter.addAll(listBaseDataBean.getData());
                }


            }

            @Override
            public void onFail(int errorCode) {
                xRefreshView.stopRefresh();
                xRefreshView.setLoadComplete(false);
            }
        });
    }

    @OnClick(R.id.close_gg)
    public void onViewClicked() {
        adLL.setVisibility(View.GONE);
    }


    private void bannerList() {
        Map map = new HashMap();
        map.put("site", "5");

        HttpMehtod.getInstance().bannerList(map, new IdeaObserver<BaseDataBean<ArrayList<BannerListDataBean>>>() {

            @Override
            public void onSuccess(final BaseDataBean<ArrayList<BannerListDataBean>> arrayListBaseDataBean) {

                if (arrayListBaseDataBean.getData() != null && arrayListBaseDataBean.getData().size() > 0) {
                    adSimdraweeview.setImageURI(arrayListBaseDataBean.getData().get(0).getImgsrc());
                    adSimdraweeview.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            if (!TextUtils.isEmpty(arrayListBaseDataBean.getData().get(0).getLink())) {
                                Intent intent = new Intent(UnusedGoodsActivity.this, H5Activity.class);
                                intent.putExtra(H5Activity.URL_KEY, arrayListBaseDataBean.getData().get(0).getLink());
                                startActivity(intent);
                            }


                        }
                    });
                } else {
                    adSimdraweeview.setVisibility(View.GONE);
                    closeGg.setVisibility(View.GONE);
                    adLL.setVisibility(View.GONE);
                }

            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }
}
